package weekone;

import java.util.Stack;

public class EvalRPN_150 {

    public int evalRPN(String[] tokens) {

        Stack<Integer> stack = new Stack<>();
        for (String s : tokens) {
            if (Character.isDigit(s.charAt(s.length()-1))) {
                stack.push(Integer.parseInt(s));
            } else {
                int a = stack.pop();
                int b = stack.pop();
                stack.push(calc(a, b, s));
            }
        }

        return stack.peek();
    }

    private int calc(Integer a, Integer b, String s) {
        int res = 0;
        switch (s) {
            case "+":
                res = a + b;
                break;
            case "-":
                res = b - a;
                break;
            case "*":
                res = b * a;
                break;
            case "/":
                res = b / a;
                break;
        }
        return res;
    }

    public static void main(String[] args) {
        String[] strings = {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        System.out.println(new EvalRPN_150().evalRPN(strings));
    }
}
